package xc.controller;



import xc.dao.ProgramDao;
import xc.entity.Program;
import xc.service.ProgramService;
import xc.utils.GetNumber;
import xc.utils.Menu;

import java.sql.SQLException;
import java.util.Scanner;

public class ProgramConteoller {

    private ProgramService programService;
    private ProgramDao programDao;
    private Scanner scanner;
    private GetNumber getNumber;

    public  ProgramConteoller() {
        this.getNumber = new GetNumber();
        this.programService = new ProgramService();
        this.programDao = new ProgramDao();
        this.scanner = new Scanner(System.in);
    }

    public void functionPragram() throws SQLException {
        while(true){
            Menu.choosePragram();

            int option = GetNumber.ToInt();
            switch (option){
                case 1://增加项目
                    addProgram();
                    break;
                case 2://删除项目
                    deleteProgram();
                    break;
                case 3://修改项目名
                    updateProgram();
                    break;
                case 4://查询项目
                    selectProgram();
                    break;
                case 5://退出
                    System.out.println("成功退出");
                    return;
            }
        }
    }

    private void selectProgram() throws SQLException {
        ProgramService.selectProgram();
    }

    private void updateProgram() throws SQLException {
        System.out.println("请输入你需要修改的项目");
        String programName = scanner.nextLine();
        System.out.println("请输入新的价格");
        String programSale = scanner.nextLine();
        Program program = new Program(programName,programSale);
        if(ProgramService.updateProgram(program)){
            System.out.println("谢谢使用");
        }else {
            System.out.println("谢谢使用");
        }
    }

    private void deleteProgram() throws SQLException {
        System.out.println("请输入需要删除的项目");
        String programName = scanner.nextLine();
        if (ProgramService.deleteProgram(programName)) {
            System.out.println("删除成功");
        }else  {
            System.out.println("删除失败");
        }
    }

    private void addProgram() throws SQLException {
       System.out.println("请输入需要添加的项目");
       String programName = scanner.nextLine();
       System.out.println("情输入该项目的价格");
       String programSale = scanner.nextLine();
       Program program = new Program(programName,programSale);
        if (ProgramService.addProgram(program)) {
            System.out.println("添加成功");
        }else  {
            System.out.println("添加失败");
        }
    }

}
